package cn.work.Filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
    禁用浏览器缓存
 */
@WebFilter("/*") // 应用到所有请求，也可以指定路径
public class CacheControlFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 将响应转换为 HttpServletResponse
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        // 设置缓存相关的响应头
        httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
        httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
        httpResponse.setHeader("Expires", "0"); // Proxies

        // 继续执行请求链
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {}
}
